<?php
include_once("../includes/page_utf_class.php");
include_once("../module/product/includes/plugin_order_class.php");
include("../lang/cn/company_type_config.php");
$order = new order;
//=======================
if($_GET['act']=='getOrderProductList' && isset($_GET['order_id']) && is_numeric($_GET['order_id'])){
	$res = $order->get_order_pro($_GET['order_id']);
	$str = '<tr><td colspan="16"><table width="100%" border="1" cellspacing="0" cellpadding="0">
      <tr id="title">
        <td>编号</td>
        <td>产品ID</td>
		<td>产品图片</td>
        <td>产品</td>
        <td>单价</td>
        <td>订单数量</td>
        <td>微仓数量</td>
        <td>总数量</td>
        <td>总价</td>
        <td>备货期</td>
        <td>出库状态</td>
      </tr>';
	
	if(empty($res)){
		$str .="<tr><td colspan='16' align='center'>未找到任何商品</td></tr>";
	}else{
		foreach($res as $val){
			$pic = "<a href=\"{$val['pic']}\" target=\"_blank\"><img width='50' height='50' src=\"{$val['pic']}/50\" /></a>";
			$is_stocked_str = $val['is_stocked']=='-1'?'未出库':($val['is_stocked']=='1'?'已出库':'未处理');
			$str .= '      <tr id="content" order_id='.$val['order_id'].'>
	        <td>'.$val["code"].'</td>
	        <td>'.$val["pid"].'</td>
	        <td>'.$pic.'</td>
	        <td><a target="_blank" href="http://wx.52ywy.com/item-'.$val['pid'].'.html">'.$val["name"].'</a></td>
	        <td>'.round($val['price'],2).'</td>
	        <td>'.($val["num"]-$val["microstock_num"]).'</td>
	        <td>'.$val["microstock_num"].'</td>
	        <td>'.$val["num"].'</td>
	        <td>'.round($val['sum'],2).'</td>
	        <td>'.$val['waiting_time_desc'].'</td>
	        <td>'.$is_stocked_str.'</td>
	      </tr>';
		}
	}
	
	$str .= "</table></td></tr>";
	die($str);
}
if(isset($_GET['orderid'])&&is_numeric($_GET['orderid']))
{
    if ($_GET['act']=='reback'){
        $order->set_order_statu($_GET['orderid'],6,$_SESSION['ADMIN_USER']);
    }else{
	    $order->set_order_statu($_GET['orderid'],4,$_SESSION['ADMIN_USER']);
	    $sql="update ".ORDER." set status='4',finished_time=".time()." where order_id='{$_GET['orderid']}'";
	    $db->query($sql);
    }
    unset($_GET['m']);
    unset($_GET['s']);
    unset($_GET['orderid']);
    unset($_GET['act']);
    $param = "";
    if(count($_GET)){
    	$param .= "&".http_build_query($_GET);
    }
	msg("module.php?m=product&s=order.php".$param);
}
if($_GET['code'])
{
	$_GET['code'] = trim($_GET['code']);
	$str.=" and  a.order_id like '$_GET[code]%' ";	
}
if($_GET['onlytoday']){
	$str .= " and a.status>='2'";
	switch ($_GET['onlytoday'])
	{
	    case 1://今天
	        $time = strtotime(date('Y-m-d'));
	        $str .= " and a.order_time>='{$time}'";
	        break;
	    case 2://昨天
	        $time = strtotime(date('Y-m-d',strtotime('-1 day')));
	        $rtime = $time+86399;
	        $str .= " and a.order_time>='{$time}' and a.order_time<='{$rtime}'";
	        break;
	    case 3://本月
	        $time = strtotime(date('Y-m'));
	        $str .= " and a.order_time>='{$time}'";
	        break;
	    default://全部
	        break;
	}
}else{
	if(isset($_GET['status']) and is_numeric($_GET['status']))
	{
		$status=$_GET['status']-1;
		$str.=" and  a.status ='$status' ";
	}else{
		$str.=" and  a.status >'0' ";
	}
}
if($_GET['user'])
{
	$sqluser="select userid from ".MEMBER." where  username like '%$_GET[user]%'";
	$db->query($sqluser);
	$userid=$db->fetchField('userid');
	$str.=" and a.member_id='$userid'";
}
if($_GET['userid']){
	$str.=" and a.member_id='$_GET[userid]'";
}
if($_GET['ship_name'])
{
	$str.=" and a.ship_name like '%$_GET[ship_name]%' ";
}
if($_GET['ship_addr'])
{
	$str.=" and a.ship_addr like '%$_GET[ship_addr]%' ";
}
if($_GET['order_ship_status'])
{
	$str.=" and a.ship_status = '$_GET[order_ship_status]' ";
}
if($_GET['order_type']){
	$str.=" and a.order_type='$_GET[order_type]' ";
}
if($_GET['order_pay_status'])
{
	$str.=" and a.pay_status = '$_GET[order_pay_status]' ";
}
if($_GET['pay'])
{
	$str.=" and a.payment_id = '$_GET[pay]' ";
}
if($_GET['client_platform']!=""){
	$str.=" and a.client_platform = '$_GET[client_platform]' ";
}
if($_GET['client_system']!=""){
	$str.=" and a.client_system = '$_GET[client_system]' ";
}
if($_GET['ship'])
{
	$str.=" and a.shipping_id  = '$_GET[ship]' ";
}
if($_GET['maxp'])
{
	$str.=" and a.cost+a.freight <= '$_GET[maxp]' ";
}
if($_GET['stime'])
{
	$str.=" and IFNULL(a.order_time,0) > '".strtotime($_GET[stime])."' ";
}
if($_GET['etime'])
{
	$str.=" and IFNULL(a.order_time,0) < '".(strtotime($_GET[etime])+86399)."' ";
}
if($_GET['p_stime'])
{
	$str.=" and IFNULL(a.pay_time,0) > '".strtotime($_GET[p_stime])."' ";
}
if($_GET['p_etime'])
{
	$str.=" and IFNULL(a.pay_time,0) < '".(strtotime($_GET[p_etime])+86399)."' ";
}
if($_GET['s_stime'])
{
	$str.=" and IFNULL(a.ship_time,0) > '".strtotime($_GET[s_stime])."' ";
}
if($_GET['s_etime'])
{
	$str.=" and IFNULL(a.ship_time,0) < '".(strtotime($_GET[s_etime])+86399)."' ";
}
if($_GET['minp'])
{
	$str.=" and a.cost+a.freight >= '$_GET[minp]' ";
}
if($_GET['shipping_no']){
    $str.=" and a.shipping_no='{$_GET['shipping_no']}'";
}
if($_GET['separate_status']=='0'){
    $str.=" and a.separate_status=0";
}
if($_GET['separate_status']=='-1'){
    $str.=" and a.separate_status<0";
}
//----------------------分仓
$general_shop =$order->get_general_shop();

if ($general_shop!=0&&$general_shop!=$_GET['stock_shop']&&isset($_GET['stock_shop'])){
	msg('noright.php');
	exit;
}
if ($general_shop){
	$_GET['stock_shop'] =$general_shop;
}
if ($_GET['stock_shop']){
	$str.=" and a.stock_shop = '$_GET[stock_shop]' ";
}
//-----------------

//查询总金额的条件
$amountCond = "";
$amountCond = str_replace('a.', '', $str);
$amountCond = str_replace("'", "\'", $amountCond);
$tpl->assign('amountCond',$amountCond);
/*
 * 判断不是admin管理员账号登录 就只能获取当前登录账号的数据
 */
if($_SESSION['ADMIN_USER']=='admin'){
	$export_sql=$sql="select a.shipping_idcard,a.ship_addr,a.ship_mobile,a.ship_zip,a.member_id,a.status,a.print_status,a.order_id,a.master_order_id,a.separate_status,a.client_system,a.client_platform,a.stock_shop,a.order_type,(a.cost+a.freight+a.tax_price+a.insurance_fee-a.cpns_price-a.discount) as sum,a.discount,a.cpns_price,a.cost,a.freight,a.cash_price,a.tax_price,a.insurance_fee,b.username as user,b.name as nick_name,b.mobile,b.email,a.ship_name,a.order_time,a.pay_time,a.ship_time,a.finished_time,a.pay_status,a.ship_status,a.shipping_name,a.payment_name,a.shipping_no,a.ms_id from ".ORDER." a left join ".MEMBER." b on b.userid=a.member_id where 1 $str order by a.id desc";
}else{
	$user = $_SESSION['ADMIN_USER'];
	$asql = "select * from haiou_admin where user='$user'";
	$db->query($asql);
	$info = $db->fetchRow();
	$group_id = $info['group_id']; //分组id
	$asql = "select * from haiou_admin_group where group_id='$group_id'";
	$db->query($asql);
	$group_info = $db->fetchRow();
	if($group_info['group_name']=='商家'){
		$seller_id = $info['id'];
		$export_sql=$sql="select a.shipping_idcard,a.ship_addr,a.ship_mobile,a.ship_zip,a.member_id,a.status,a.print_status,a.order_id,a.master_order_id,a.separate_status,a.client_system,a.client_platform,a.stock_shop,a.order_type,(a.cost+a.freight+a.tax_price+a.insurance_fee-a.cpns_price-a.discount) as sum,a.discount,a.cpns_price,a.cost,a.freight,a.cash_price,a.tax_price,a.insurance_fee,b.username as user,b.name as nick_name,b.mobile,b.email,a.ship_name,a.order_time,a.pay_time,a.ship_time,a.finished_time,a.pay_status,a.ship_status,a.shipping_name,a.payment_name,a.shipping_no,a.ms_id from ".ORDER." a left join ".MEMBER." b on b.userid=a.member_id where a.seller_id=$seller_id $str order by a.id desc";
	}else{
		$export_sql=$sql="select a.shipping_idcard,a.ship_addr,a.ship_mobile,a.ship_zip,a.member_id,a.status,a.print_status,a.order_id,a.master_order_id,a.separate_status,a.client_system,a.client_platform,a.stock_shop,a.order_type,(a.cost+a.freight+a.tax_price+a.insurance_fee-a.cpns_price-a.discount) as sum,a.discount,a.cpns_price,a.cost,a.freight,a.cash_price,a.tax_price,a.insurance_fee,b.username as user,b.name as nick_name,b.mobile,b.email,a.ship_name,a.order_time,a.pay_time,a.ship_time,a.finished_time,a.pay_status,a.ship_status,a.shipping_name,a.payment_name,a.shipping_no,a.ms_id from ".ORDER." a left join ".MEMBER." b on b.userid=a.member_id where 1 $str order by a.id desc";
	}
}

//=============================
$page = new Page;
$page->listRows=20;
if (!$page->__get('totalRows')){
	$db->query($sql);
	$de['count']=$page->totalRows = $db->num_rows();
}
$de['count']=$de['count']?$de['count']:$_GET['totalRows'];
$sql .= "  limit ".$page->firstRow.",".$page->listRows;
$de['page'] = $page->prompt();
//=============================

if ($_POST['act']=='export'){
    include_once '../lib/php_excel.php';
    $db->query($export_sql);
    $export_data_temp=$db->getRows();

	$listCount = 0;

    foreach ($export_data_temp as $key=> $var)
    {
    	$orderMain['member_id'] = $var['member_id'];
    	if($var['status'] == '0')
    	{
    		$orderMain['status'] = '已取消';
    	}elseif ($var['status'] == '2')
    	{
    		$orderMain['status'] = '待发货';
    	}elseif($var['status'] == '1')
    	{
    		$orderMain['status'] = '待付款';
    	}elseif($var['status'] == '3')
    	{
    		$orderMain['status'] = '待收货';
    	}elseif($var['status'] == '4')
    	{
    		$orderMain['status'] = '已完成';
    	}
    	$orderMain['order_id'] = $var['order_id'];
    	$orderMain['sum'] = $var['sum'];
    	$orderMain['cost'] = $var['cost'];
    	$orderMain['freight'] = $var['freight'];
    	$orderMain['nick_name'] = $var['nick_name'];
    	$orderMain['ship_name'] = $var['ship_name'];
    	$orderMain['ship_mobile'] = $var['ship_mobile'];
    	$orderMain['ship_addr'] = $var['ship_addr'];
    	$orderMain['ship_zip'] = $var['ship_zip'];
    	$orderMain['shipping_idcard'] = $var['shipping_idcard'];
    	$orderMain['order_time'] = $var['order_time']?date('Y-m-d H:i:s',$var['order_time']):"";
    	$orderMain['pay_status']=$order_pay_status[$var['pay_status']-1];
    	$orderMain['pay_time']=$var['pay_time']?date("Y-m-d H:i:s",$var['pay_time']):"";
    	$orderMain['ship_status']=$order_ship_status[$var['ship_status']-1];
    	$orderMain['ship_time']=$var['ship_time']?date("Y-m-d H:i:s",$var['ship_time']):"";
    	$orderMain['shipping_name'] = $var['shipping_name'];
    	$orderMain['payment_name'] = $var['payment_name'];
    	$orderMain['shipping_no'] = $var['shipping_no'];
    	$orderMain['client_platform'] = $client_platform[$var['client_platform']];
    	$orderMain['finished_time']=$var['finished_time']?date("Y-m-d H:i:s",$var['finished_time']):"";

		// 获取订单产品信息
		$orderId = $var['order_id'];
		$sql_products = "SELECT * FROM " . ORPRO . " WHERE order_id='{$orderId}'";

		$db->query($sql_products);
		$order_products = $db->getRows();

		foreach ($order_products as $order_product) {
			$export_data[$listCount] = $orderMain;
			$export_data[$listCount]['product_name'] = $order_product['product_name'];
			$export_data[$listCount]['product_id'] = $order_product['product_id'];
			$export_data[$listCount]['property'] = $order_product['property'];
			$export_data[$listCount]['price'] = $order_product['price'];
			$export_data[$listCount]['num'] = $order_product['num'];
			$export_data[$listCount]['ms_discount'] = $order_product['ms_discount'];

			$listCount++;
		}


    }
    $queue[0] = array("会员ID","订单状态", "订单编号","订单总额","商品总价","配送费","买家名称","收货人","收货人电话",
			"收货地址","收货人邮编","身份证号","下单时间","支付状态","支付时间","配送状态","发货时间","配送方式","支付方式",
			"快递单号","订单类型","订单完成时间","产品名称","产品ID","规格","价格","数量","折扣");//定义表头
    $export_data = array_merge($queue,$export_data);
    //print_r($export_data);
    $filename =   "订单信息" . date ( "YmdHis" );
    $xls = new Excel_XML ( 'UTF-8', false, '订单信息' );
    $xls->addArray ( $export_data );
    $xls->generateXML ( $filename );
    exit ();
}
$db->query($sql);
$ss=$db->getRows();
foreach($ss as $k)
{
// 	$k['sum']=$k['cost']+$k['freight'];
	$k['statu_text']=$order->get_shop_order_statu($k['status']);
	$k['order_pay_status']=$order_pay_status[$k['pay_status']-1];
	$k['order_ship_status']=$order_ship_status[$k['ship_status']-1];
	$k['inventory_status']=$order->get_inventory_status($k['order_id'])?0:1;
	$k['pay_time'] = $k['pay_time']?date('m-d H:i',$k['pay_time']):'';
	$k['ship_time'] = $k['ship_time']?date('m-d H:i',$k['ship_time']):'';
	$k['finished_time'] = $k['finished_time']?date('m-d H:i',$k['finished_time']):'';
	$list[]=$k;
}
$de["list"]=$list;

$condTemp = $_GET;
unset($condTemp['m']);
unset($condTemp['s']);
$cond = "";
if(count($condTemp)){
	$cond = "&".http_build_query($condTemp);
}
$tpl->assign("cond",$cond);

$tpl->assign("order_shop_status",$order_shop_status);
$tpl->assign("order_ship_status",$order_ship_status);
$tpl->assign("order_pay_status",$order_pay_status);
$tpl->assign("de",$de);
$sql = "select id,title from ".LGSTEMP;
$db->query($sql);
$ship = $db->getRows();
$tpl->assign("ship",$ship);
$sql = "select payment_id,payment_name from ".PAYMENT;
$db->query($sql);
$pay = $db->getRows();
$stock_shops=$order->get_stock_shops();
$tpl->assign("pay",$pay);
$tpl->assign("config",$config);
$tpl->assign("client_system",$client_system);
$tpl->assign("client_platform",$client_platform);
$tpl->assign("stock_shops",$stock_shops);
$tpl->assign("general_shop",$general_shop);
$tpl->display("order.htm");

?>